package com.java2104.controller;

import com.github.pagehelper.PageInfo;
import com.java2104.entity.Website;
import com.java2104.service.WebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/website")
public class WebsiteController {
    @Autowired
    private WebsiteService websiteService;

    @RequestMapping("/index")
    public String index() {
        return "website/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<Website> list(String name, String country,
                                  @RequestParam(name = "pn", defaultValue = "1") int pageNum,
                                  @RequestParam(name = "ps", defaultValue = "5") int pageSize) {

        return websiteService.search(name, country, pageNum, pageSize);

    }

    @RequestMapping("/countryList")
    @ResponseBody
    public List<String> countryList() {
        return websiteService.searchCountry();
    }

    @GetMapping("/add") //实现跳转到 add.jsp
    public void add() {}

    @PostMapping("/add") //实现提交站点数据
    public String add(Website website) {
        websiteService.save(website);
        return "redirect:/website/index";
    }

    @PostMapping("/delete") //删除方法
    @ResponseBody
    public String delete(int id) {
        if (websiteService.remove(id))
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/access")
    public void access(int siteId) {
        websiteService.access(siteId);
    }
}
